<?php
namespace app\admin\controller;
use app\admin\model\BatteryGoods;
use think\Controller;
use think\Db;
use think\Request;

// 商品控制
class Battery extends Common
{
    // 查看全部商品列表
    public function getGoodsList(Request $request)
    {

        $goods_cate = $request->param('goods_cate',0);
        $query = BatteryGoods::getGoodsListQuery($goods_cate);
        
        $list = $query->order('goods_id desc')->paginate(5);
       
        $this->assign('goods_amount', BatteryGoods::getGoodsCount($goods_cate));
        $this->assign('page', $list->render());
        $this->assign('list', $list);
        return $this->fetch('battery/goods');
    }



    // 新上架商品
    public function getAddGoods(){

        return $this->fetch('battery/add');
    }

    // 
    public function postAddGoods(Request $request){

        if($request->isPost()){
            $post_data = $request->post();

           if(empty($post_data['goods_name'])){
              $this->error('商品名称不能为空','/battery/AddGoods');
            }
            if(empty($post_data['price'])) {
                $this->error('商品价格不能为空', '/battery/AddGoods');
            }
            if (empty($post_data['stock'])) {
                $this->error('当前库存不能为空', '/battery/AddGoods');
            }

            $goods_info = [
                'goods_name'=>$post_data['goods_name'],
                'goods_sn'=>$post_data['goods_sn'],
                'price'=>$post_data['price'],
                'stock'=>$post_data['stock'],
                'goods_cate'=>0,
                'goods_type'=>$post_data['goods_type'],
                'formal_price'=>$post_data['formal_price'],
                'describe'=>nl2br($post_data['describe']),
                'is_show'=>$post_data['is_show']
            ];
            // 商品图片
            $file=$request->file('goods_img');
            if($file){
                
                $goods_info['goods_img'] =  handle_upload_image($file,BatteryGoods::GOODS_IMAGE_PATH);
                
            } else {
                $goods_info['goods_img'] = 'default.jpg';

            }

            $bool = Db::name(BatteryGoods::TABLE_NAME)->insert($goods_info);

            if ($bool) {
               $this->success('添加成功', '/battery/GoodsList');
            } else {
               $this->error('添加失败', '/battery/AddGoods');
            }
        }
        

    }


    // 商品信息修改
    public function getEditGoods(Request $request){
        
        $id=$request->param('id');


        $goodsInfo=BatteryGoods::getGoodsInfoById($id);

        $goodsInfo['goods_img']=get_image_url(BatteryGoods::GOODS_IMAGE_PATH,$goodsInfo['goods_img']);
        return $this->fetch('battery/edit',['info'=>$goodsInfo]);
    }

    public function postEditGoods(Request $request){
        // 更新商品信息
        if($request->isPost()){
            $post_data=$request->post();

            $goods_info = [
                'goods_name'=>$post_data['goods_name'],
                'goods_sn'=>$post_data['goods_sn'],
                'price'=>$post_data['price'],
                'stock'=>$post_data['stock'],
                'goods_cate'=>0,
                'goods_type'=>$post_data['goods_type'],
                'formal_price'=>$post_data['formal_price'],
                'describe'=>nl2br($post_data['describe']),
                'is_show'=>$post_data['is_show']
            ];

            $file=$request->file('goods_img');
            $change_img =false;
            if($file){

                $change_img =true;
                $goods_info['goods_img'] =  handle_upload_image($file,BatteryGoods::GOODS_IMAGE_PATH);
                // 原来图片地址
                $oldImgPath= BatteryGoods::getGoodsInfoById($post_data['goods_id'],'goods_img');

            }

            $res = Db::name(BatteryGoods::TABLE_NAME)->where(['goods_id'=>$post_data['goods_id']])
                 ->update($goods_info);

            if ($res) {
                if($change_img && $oldImgPath['goods_img'] != 'default.jpg'){
                    unlink(get_image_url(BatteryGoods::GOODS_IMAGE_PATH,$oldImgPath['goods_img'],false));
                }

               $this->success('修改成功', '/battery/GoodsList');
            } else {
                 $this->error('修改失败', "/battery/EditGoods/id/{$post_data['goods_id']}");
            }

        }

        

    }

    // Ajax 下架 下架切换
    public function postAjaxWithdrawGoods(Request $request){

        if($request->isAjax()){
            $is_show = $request->param('is_show');
            $goods_id=$request->param('goods_id');


            return BatteryGoods::updateGoodsStatus($is_show,$goods_id);

        }else{
            return false;
        }
    }

    // 删除商品信息
    public function postDelGoods(Request $request){
        $id=$request->post('id');

        $res=BatteryGoods::delGoodsInfo($id);

        return $res;
    }

}
